package com.globalwave.system.service;

import org.apache.commons.lang.StringUtils;

import com.globalwave.base.BaseServiceImpl;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.system.entity.Dict;
import com.globalwave.system.entity.DictCriterion;

public class DictServiceImpl extends BaseServiceImpl implements DictService {

    @SuppressWarnings("unchecked")
    public ArrayPageList<Dict> query(
            Dict dictCriterion, 
            int pageIndex, 
            int pageSize,
            ArrayOrderList orderList) 
    throws Exception {
    	
    	if (dictCriterion != null) {
	    	if (StringUtils.isEmpty(dictCriterion.getCode_())) {
	    		dictCriterion.setCode_(null) ;
	    	}
	    	
	    	if (StringUtils.isEmpty(dictCriterion.getDomain_())) {
	    		dictCriterion.setDomain_(null) ;
	    	}
	    	
	    	if (StringUtils.isEmpty(dictCriterion.getName_())) {
	    		dictCriterion.setName_(null) ;
	    	}
	    	
	    	if (StringUtils.isEmpty(dictCriterion.getStatus_())) {
	    		dictCriterion.setStatus_(null) ;
	    	}
    	}
    	
    	if (orderList == null) {
    		orderList = new ArrayOrderList() ;
    		orderList.addAsc("domain_") ;
    		orderList.addAsc("order_") ;
    	}
    	
        return (ArrayPageList<Dict>)jdbcDao.query(dictCriterion, pageIndex, pageSize, orderList, Dict.class);
    }

    public boolean checkDictExisted(Dict dict) throws Exception {
        return jdbcDao.find(dict) != null;
        
    }

    public Dict create(Dict dict) throws Exception { 
        final Dict newDict = (Dict) jdbcDao.insert(dict) ;
                
        return newDict;
    }

    public void update(Dict dict) throws Exception {        
        jdbcDao.update(dict);
    }

    public void delete(Dict dict) throws Exception {
        
        jdbcDao.delete(dict) ;
        
    }

    public void deleteAll(Long[] dictIds) throws Exception {
        
        DictCriterion criterion = new DictCriterion() ;
        criterion.setDictIds(dictIds) ;

        jdbcDao.delete(Dict.class, criterion) ;
        
    }

    public Dict find(Long dict_id) throws Exception {
        Dict dict = new Dict() ;
        dict.setId(dict_id) ;
        
        return (Dict)jdbcDao.find(dict) ;
    }


}
